﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;


namespace WyoCodeTools.Classes
{
    public class VSConfigFile
    {
        //      <connectionStrings>
        //  <add name="oe_timetrackrConnectionString" connectionString="Data Source=72.18.156.74,1533;Initial Catalog=oe_timetrackr;Persist Security Info=True;User ID=dataAdmin;Password=kendall1383;Packet Size=4096"
        //    providerName="System.Data.SqlClient" />
        //</connectionStrings>

        public string Name { get; set; }

        public bool RootCFG { get; set; }

        public string FileLoc { get; set; }

        XElement Config;

        public Dictionary<string, string> ConnectionStrings { get; set; }

        public VSConfigFile(string file)
        {
            Name = Path.GetFileName(file);
            if (Path.GetExtension(Name) == ".config")
            {
                RootCFG = true;
            }
            FileLoc = file;
            ConnectionStrings = new Dictionary<string, string>();
            Config = XElement.Load(file);
            Init();
        }

        private void Init()
        {
            try
            {
                foreach (XElement e in Config.Descendants("connectionStrings").First().Descendants("add"))
                {
                    ConnectionStrings.Add(e.Attribute("name").Value, e.Attribute("connectionString").Value);
                }
            }
            catch { }
        }



        internal void UpdateWCFService(string namspace, string wcfname)
        {
            //    <binding name="SilverlightApplication2.Web.SLTestData.customBinding0">
            //  <binaryMessageEncoding />
            //  <httpTransport />
            //</binding>


            //            <service name="SilverlightApplication2.Web.SLTestData">
            //  <endpoint address="" binding="customBinding" bindingConfiguration="SilverlightApplication2.Web.SLTestData.customBinding0"
            //    contract="SilverlightApplication2.Web.SLTestData" />
            //  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            //</service>



            TemplateEngine cfg = new TemplateEngine(File.ReadAllText(@"Templates\ServiceModelTemplate.txt"));
            cfg.SetVar("namespace", namspace);
            cfg.SetVar("wcfname", wcfname);


                Config.Add(XElement.Parse(cfg.ToString()));
                //var bindingtoremove = svcele.Descendants("endpoint").Where(sl => sl.Attribute("bindingConfiguration").Value.Contains("customBinding"));
                //foreach (var b in bindingtoremove)
                //{

                //    Config.Descendants("binding").First(e => e.Attribute("name").Value == b.Attribute("bindingConfiguration").Value).Remove();

                //}
                //svcele.Remove();
                Config.Save(FileLoc);
            


        }

        internal void AddLogging()
        {
            try
            {
                if (Config.Descendants("system.diagnostics").Count() > 0)
                {
                    Config.Descendants("system.diagnostics").First().Remove();
                }
            }
            finally
            {
                Config.Add(XElement.Parse("<system.diagnostics><trace autoflush=\"true\" /><sources><source name=\"System.ServiceModel\" switchValue=\"Error\" propagateActivity=\"true\"><listeners><add name=\"sdt\" type=\"System.Diagnostics.XmlWriterTraceListener\" initializeData=\"WyoApp.svclog\" /></listeners></source></sources></system.diagnostics>"));
                Config.Save(FileLoc);
            }
        }


        internal void RemoveWCFService()
        {
            //    <binding name="SilverlightApplication2.Web.SLTestData.customBinding0">
            //  <binaryMessageEncoding />
            //  <httpTransport />
            //</binding>


            //            <service name="SilverlightApplication2.Web.SLTestData">
            //  <endpoint address="" binding="customBinding" bindingConfiguration="SilverlightApplication2.Web.SLTestData.customBinding0"
            //    contract="SilverlightApplication2.Web.SLTestData" />
            //  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            //</service>



            //TemplateEngine cfg = new TemplateEngine(File.ReadAllText(@"Templates\ServiceModelTemplate.txt"));
            //cfg.SetVar("namespace", namspace);
            //cfg.SetVar("wcfname", wcfname);

            try
            {
                if (Config.Descendants("system.serviceModel").Count() > 0)
                {
                    Config.Descendants("system.serviceModel").First().Remove();
                }
            }
            finally
            {
                //Config.Add(XElement.Parse(cfg.ToString()));
                //var bindingtoremove = svcele.Descendants("endpoint").Where(sl => sl.Attribute("bindingConfiguration").Value.Contains("customBinding"));
                //foreach (var b in bindingtoremove)
                //{

                //    Config.Descendants("binding").First(e => e.Attribute("name").Value == b.Attribute("bindingConfiguration").Value).Remove();

                //}
                //svcele.Remove();
                Config.Save(FileLoc);
            }


        }
    }
}
